<?php
@session_start();
require_once("../model/user.php");

if(isset($_POST['register']))
{


    $obj_user = new User(); // khoi tao doi tuong user

    // thiet lap lai cac bien

    $security_code = isset($_POST['capcha']) ? $_POST['capcha'] : null;
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $telephone = $_POST['telephone'];
    $address = $_POST['address'];
    $gender = $_POST['gender'];

    // xu ly file upload len
    $file_name  = $_FILES['file_upload']['name'];
    $file_type  = $_FILES['file_upload']['type'];
    $file_size  = $_FILES['file_upload']['size'];
    $file_path  = $_FILES['file_upload']['tmp_name'];
    $file_error = $_FILES['file_upload']['error'];

    if($security_code != $_SESSION["security_code"] || !$security_code)
    {
        echo "Nhap sai ki tu bao mat";
        exit();
    }

    // xy ly loi tra ve
    if($file_error  > 0)  {
        echo "File error";
        exit();
    }

    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file_upload"]["name"]);
    $extension = end($temp); // chen extension vao cuoi ten file

    // kiem tra dinh dang file anh
    if($file_type == "image/jpeg" || $file_type == "image/png" || $file_type == "image/jpg" || $file_type == "image/gif" && in_array($extension, $allowedExts))
    {
        if(file_exists("../upload/".$file_name)){
            echo "File đã tồn tại";
        }
        else
        {
            if($obj_user->insert_user_into_db($username, $password,'user',$address, $gender, $telephone, $email, $file_name ) == true)
            {
                echo "Success";
                move_uploaded_file($file_path, "../upload/".$file_name);
            }
        }
    }
}
